//
//  KeHuXiangXinXiViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/17.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "KeHuXiangXinXiViewController.h"
#import "XiangXiXinXiTableViewCell.h"
#import "XiangXiXinXiHeaderView.h"
@interface KeHuXiangXinXiViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *dataSource;
    NSMutableArray *xiTongXiaoXiArray;
    NSArray *resultArray;
    NSArray *biaoTouArray;
}

@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;
@end

@implementation KeHuXiangXinXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.hidden = YES;
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
        self.mainTableView.sectionHeaderTopPadding = 0;
    }
    [_mainTableView registerNib:[UINib nibWithNibName:@"XiangXiXinXiTableViewCell" bundle:nil] forCellReuseIdentifier:@"XiangXiXinXiTableViewCell"];
//    
    _mainTableView.estimatedRowHeight = 44;//这里指定预设高度，虽然没什么用，但是必须指定
    _mainTableView.rowHeight = UITableViewAutomaticDimension;
    
    if(_isGongHai){
        [self huoQuGongHaiBiaoTou];
    }else{
        [self huoQuBiaoTou];
    }
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getInfoFromServer];
    }];

    _mainTableView.mj_header = header;
}
//获取客户表头信息
- (void)huoQuBiaoTou{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
//    [paraDic setValue:@"2" forKey:@"types"];
    
    [paraDic setValue:@"2" forKey:@"label"];
    
    [paraDic setValue:_customerId forKey:@"id"];
    NSString *url = @"/crmField/queryListHead/2";
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:url andParas:nil andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            biaoTouArray = [info objectForKey:@"data"];

            [self getInfoFromServer];
        }
    }];
}
//获取公海表头信息
- (void)huoQuGongHaiBiaoTou{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    NSString *url = @"/crmCustomerPool/queryPoolListHead";
    [paraDic setValue:_poolId forKey:@"poolId"];
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:url andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            biaoTouArray = [info objectForKey:@"data"];
            
            [self getInfoFromServer];
        }
    }];
}





//获取客户详细信息
- (void)getInfoFromServer{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"2" forKey:@"types"];
    [paraDic setValue:_customerId forKey:@"id"];
//    NSString *url = [NSString stringWithFormat:@"/crmCustomer/information/%@",_customerId];
    NSString *url = [NSString stringWithFormat:@"/crmCustomerAll/information/%@",_customerId];
    
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:url andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            resultArray = [info objectForKey:@"data"];
        
            dataSource = [NSMutableArray array];
            xiTongXiaoXiArray = [NSMutableArray array];
            
            for (NSDictionary *dataDic in biaoTouArray) {
                NSDictionary *temp = [self huoQuJieGuoDicWithFieldName:GetHandleString(dataDic, @"fieldName")];
                if (temp.allValues.count > 0) {
                    if([GetHandleString(temp, @"sysInformation") integerValue] == 1){
                        if([[temp allValues] count] == 0){
                            [xiTongXiaoXiArray addObject:dataDic];
                        }else{
                            [xiTongXiaoXiArray addObject:temp];
                        }
                    }else{
                        if([[temp allValues] count] == 0){
                            [dataSource addObject:dataDic];
                        }else{
                            [dataSource addObject:temp];
                        }
                    }
                }
            }
            [_mainTableView reloadData];
        }
        
        [_mainTableView.mj_header endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    XiangXiXinXiHeaderView *header = [XiangXiXinXiHeaderView view];
    header.titleLabel.text = @[@"基本信息",@"系统信息"][section];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 1){
        return xiTongXiaoXiArray.count;
    }
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XiangXiXinXiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XiangXiXinXiTableViewCell"];
//    NSDictionary *dataDic = [self huoQuJieGuoDicWithFieldName:GetHandleString(tempDci, @"fieldName")];
    NSDictionary *dataDic = [NSDictionary dictionary];
    if(indexPath.section == 0){
        dataDic = dataSource[indexPath.row];
    }else if (indexPath.section == 1){
        dataDic = xiTongXiaoXiArray[indexPath.row];
    }
    cell.nameLabel.text = GetHandleString(dataDic, @"name");
    
    if([GetHandleString(dataDic, @"name") isEqualToString:@"距进入公海时间"]){
        NSLog(@"%@",dataDic);
    }

    if([[dataDic objectForKey:@"value"] isKindOfClass:[NSString class]] || [[dataDic objectForKey:@"value"] isKindOfClass:[NSNumber class]]){
        cell.valueLabel.text = GetHandleString(dataDic, @"value");
    }else{
        if ([GetHandleString(dataDic, @"fieldName") isEqualToString:@"createUserName"] || [GetHandleString(dataDic, @"fieldName") isEqualToString:@"ownerUserName"]) {
            
            if([[dataDic objectForKey:@"value"] isKindOfClass:[NSArray class]]){
                NSArray *array = [dataDic objectForKey:@"value"];
                if(array.count > 0){
                    cell.valueLabel.text = GetHandleString(array.firstObject, @"realname");
                }else{
                    cell.valueLabel.text = @"";
                }
            }else{
                cell.valueLabel.text = @"";
            }
        }else if ([GetHandleString(dataDic, @"fieldName") isEqualToString:@"mapAddress"]) {
            cell.valueLabel.text = GetHandleString([dataDic objectForKey:@"value"], @"address");
        }else{
            cell.valueLabel.text = @"";
        }
    }
    
    NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
    CGSize size=[cell.nameLabel.text sizeWithAttributes:attrs];
    
    cell.nameWidth.constant = size.width+8;
    
    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSDictionary *tempDci = dataSource[indexPath.row];
//    NSDictionary *dataDic = [self huoQuJieGuoDicWithFieldName:GetHandleString(tempDci, @"fieldName")];
//    AllModel *model = [[AllModel alloc] init];
//    model.modelInfoDictionary = dataDic;
//    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[XiangXiXinXiTableViewCell class] contentViewWidth:[self cellContentViewWith]];
//}


- (NSDictionary *)huoQuJieGuoDicWithFieldName:(NSString *)fieldName{
    NSDictionary *dataDic = [NSDictionary dictionary];
    for (NSDictionary *tempD in resultArray) {
        if([fieldName isEqualToString:@"address"]){
            fieldName = @"mapAddress";
        }
        if ([GetHandleString(tempD, @"name") isEqualToString:@"手机号"]) {
            NSLog(@"=========%@",tempD);
        }
        if([GetHandleString(tempD, @"fieldName") isEqualToString:fieldName]){
            dataDic = tempD;
            break;
        }
    }
    return dataDic;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end





